package com.gavin.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

/**
 * ------------------------------
 * 运行时系统信息
 * ------------------------------
 * @author wdm（l311576@sina.com）  @date 2018年7月26日
 * @version 1.0
 */
public class RuntimeInfo {
	public long lUptime = -1;
	public int nCPUCount = -1;
	public double dblLoad = -1;

	public long lMemUsed = -1;
	public long lMemCommited = -1;
	public long lMemMax = -1;

	public int nDaemonThreadCount = -1;
	public int nUserThreadCount = -1;
	public int nPeakThreadCount = -1;

	public static RuntimeInfo getMemoryInfo() {
		RuntimeInfo runtimeInfo = new RuntimeInfo();

		try {
			java.lang.management.MemoryUsage memInfo = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

			runtimeInfo.lMemUsed = memInfo.getUsed() / 1024 / 1024;
			runtimeInfo.lMemCommited = memInfo.getCommitted() / 1024 / 1024;
			runtimeInfo.lMemMax = memInfo.getMax() / 1024 / 1024;
		} catch (Exception e) {
		}

		try {
			runtimeInfo.lUptime = ManagementFactory.getRuntimeMXBean().getUptime();
			runtimeInfo.dblLoad = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();
			runtimeInfo.nCPUCount = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
			ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
			runtimeInfo.nDaemonThreadCount = threadMX.getDaemonThreadCount();
			runtimeInfo.nUserThreadCount = threadMX.getThreadCount() - threadMX.getDaemonThreadCount();
			runtimeInfo.nPeakThreadCount = threadMX.getPeakThreadCount();
		} catch (Exception e) {
		}

		return runtimeInfo;
	}
}
